'use strict';

const BaseController = require('../../core/base_controller');

class InvoiceTitleController extends BaseController {
  // 获取当前用户的发票抬头列表
  async index() {
    const { ctx } = this;
    const {user_id} = ctx.request.headers
    const data = await ctx.model.InvoiceTitle.findAll({
      where: { user_id },
      order: [['created_at', 'DESC']],
    });

    this.success(data);
  }

  // 创建发票抬头
  async create() {
    const { ctx } = this;
    const {user_id} = ctx.request.headers;

    const rules = {
      tax_identification_number: { type: 'string', required: true },
      title: { type: 'string', required: true },
      deposit_bank: { type: 'string', required: false },
      bank_account: { type: 'string', required: false },
    };

    try {
      ctx.validate(rules);
    } catch (err) {
      ctx.throw(400, '参数错误');
    }

    const data = {
      ...ctx.request.body,
      user_id,
    };
    const invoiceTitle = await ctx.model.InvoiceTitle.create(data);
    if(ctx.request.body.is_default){
      let list = await ctx.model.InvoiceTitle.findAll({
        where:{
          user_id,
          id:{
            [ctx.model.Sequelize.Op.notIn]:[invoiceTitle.dataValues.id]
          },
          is_default:true
        }
      })
      for(let item of list){
        item = item.dataValues
        await ctx.model.InvoiceTitle.update({is_default:false},{where:{id:item.id}})
      }
    }
    this.success();
  }

  // 更新发票抬头
  async update() {
    const { ctx } = this;
    const {user_id} = ctx.request.headers;
    const { id } = ctx.params;

    const invoiceTitle = await ctx.model.InvoiceTitle.findOne({
      where: {
        id,
        user_id, // 确保只能更新自己的发票抬头
      },
    });

    if (!invoiceTitle) {
      ctx.throw(404, '发票抬头不存在');
    }
    if(ctx.request.body.is_default){
      let list = await ctx.model.InvoiceTitle.findAll({
        where:{
          user_id,
          id:{
            [ctx.model.Sequelize.Op.notIn]:[invoiceTitle.dataValues.id]
          },
          is_default:true
        }
      })
      for(let item of list){
        item = item.dataValues
        await ctx.model.InvoiceTitle.update({is_default:false},{where:{id:item.id}})
      }
    }
    await invoiceTitle.update(ctx.request.body);
    this.success(invoiceTitle);
  }

  // 删除发票抬头
  async destroy() {
    const { ctx } = this;
    const {user_id} = ctx.request.headers;
    const { id } = ctx.params;

    const invoiceTitle = await ctx.model.InvoiceTitle.findOne({
      where: {
        id,
        user_id, // 确保只能删除自己的发票抬头
      },
    });

    if (!invoiceTitle) {
      ctx.throw(404, '发票抬头不存在');
    }

    await invoiceTitle.destroy();
    this.success(null, '删除成功');
  }

  // 获取发票抬头详情
  async show() {
    const { ctx } = this;
    const {user_id} = ctx.request.headers;
    const { id } = ctx.params;

    const invoiceTitle = await ctx.model.InvoiceTitle.findOne({
      where: {
        id,
        user_id, // 确保只能查看自己的发票抬头
      },
    });

    if (!invoiceTitle) {
      ctx.throw(404, '发票抬头不存在');
    }

    this.success(invoiceTitle);
  }
}

module.exports = InvoiceTitleController;
